package com.wisdom;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ViewSwitcher;

import com.wisdom.adapter.MyAdapter;
import com.wisdom.http.HttpNewsService;
import com.wisdom.http.HttpRequestUrl;
import com.wisdom.utils.ActivityUtil;
import com.wisdom.view.MyListView;
import com.wisdom.view.MyListView.OnRefreshListener;
import com.wisdom.vo.News;


public class MyCollectActivity extends Activity 
{

	private MyListView listView;

	private Map<String,String> params;
	private HttpNewsService newsService;
	private MyAdapter adapter;
	private List<News> list;

	private ViewSwitcher viewSwitcher;

	private Button button;

	private ActivityUtil activityUtil;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_mycollect);
		setTheme(android.R.style.Theme_Translucent_NoTitleBar);

		initViews();
		requestRSSFeed();

	}

	private void initViews() 
	{
		viewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher_tab_follow);
		listView = new MyListView(this);
		listView.setCacheColorHint(Color.argb(0, 0, 0, 0));
		button = new Button(this);
		button.setText("查看更多我的收藏");
		button.setHint(21+"");
		activityUtil = new ActivityUtil();
		button.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				int startIndex = Integer.parseInt(button.getHint().toString());
				int endIndex = startIndex+ActivityUtil.COUNT;
				list = activityUtil.nextPage(list, startIndex,endIndex,HttpRequestUrl.URL_FOLLOW);
				if(list==null)
				{
					ActivityUtil.toast(MyCollectActivity.this, "没有更多我的收藏加载了");
				}
				else
				{
					adapter.notifyDataSetChanged();
					button.setHint(endIndex+"");
				}
			}
		});
		listView.addFooterView(button);
		listView.setonRefreshListener(refreshListener);
		viewSwitcher.addView(listView);
		viewSwitcher.addView(getLayoutInflater().inflate(R.layout.layout_progress_page, null));
		viewSwitcher.showNext();
		listView.setOnItemClickListener(listener);
		newsService = new HttpNewsService();
		params = new HashMap<String, String>();

	}

	private OnRefreshListener refreshListener = new OnRefreshListener() {
		
		@Override
		public void onRefresh() 
		{
			new AsyncTask<Map<String,String>, Integer, List<News>>()
			{

				@Override
				protected List<News> doInBackground(Map<String, String>... params) {
					try 
					{
						Thread.sleep(1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
					data();
					return list;
				}
				@Override
				protected void onPostExecute(List<News> result)
				{
					button.setHint(21+"");
					listView.setAdapter(new MyAdapter(list, MyCollectActivity.this));
					listView.onRefreshComplete();
				}
				
			}.execute();
		}
	};

	private void requestRSSFeed() 
	{
		Thread t = new Thread()
		{
			@Override
			public void run() 
			{
				super.run();
				try 
				{
					data();
					if (list.size() == 0)
					{
						handler.sendEmptyMessage(-1);
					} 
					else 
					{
						handler.sendEmptyMessage(1);
					}
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}
			}
		};
		t.start();
	}
	private void data()
	{
		params.clear();
		params.put("news.startIndex", 1+"");
		params.put("news.endIndex", 21+"");
		String data = newsService.requestByPost(HttpRequestUrl.url(HttpRequestUrl.NEWS_COMMENTS),params);
		list = newsService.parseNews(data);
		
	}
	private Handler handler = new Handler() 
	{
		public void handleMessage(android.os.Message msg) 
		{
			if (msg.what == 1) {
				adapter = new MyAdapter(list,MyCollectActivity.this);
				listView.setOnItemClickListener(listener);
				listView.setAdapter(adapter);
				viewSwitcher.showPrevious();
			}
		};
	};

	private OnItemClickListener listener = new OnItemClickListener() 
	{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
		{
			Intent intent = new Intent(MyCollectActivity.this, NewsContentActivity.class);
			intent.putExtra("content_url", list.get(position-1).getNewsId()+"");
			intent.putExtra("count", list.get(position-1).getCommentsCount()+"");
			MyCollectActivity.this.startActivityForResult(intent, position);
		}
	};

}

